package com.hongfeng.support.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hongfeng.support.annotation.Online;
import com.hongfeng.support.common.Page;
import com.hongfeng.support.controller.common.BaseController;
import com.hongfeng.support.item.ParmItem;
import com.hongfeng.support.item.tag.FieldOperator;
import com.hongfeng.support.model.BizInfo;
import com.hongfeng.support.service.BizInfoService;
import com.hongfeng.support.util.json.JsonUtils;

/**
 * 
 * <业务介绍Controller>
 * 
 * @author llc
 * @date 创建时间：2012-12-5 下午02:26:49
 */
@Controller
@Online
public class BizInfoController extends BaseController {

	@Resource
	private BizInfoService bizInfoService;

	@RequestMapping("list")
	public void list(HttpServletRequest request, HttpServletResponse response,
			String content, Page<BizInfo> page) {
		log.info("业务介绍：" + content);
		ParmItem item = new ParmItem("content", content, FieldOperator.LIKE);
		bizInfoService.findPageByQuery(page, BizInfo.class, item);
		String json = JsonUtils.bean2Json(page);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("create")
	public void create(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("bizInfo") String bizInfoJson) {
		log.info("添加业务介绍:" + (bizInfoJson != null ? bizInfoJson : null));
		if (bizInfoJson == null) {
			this.sendErrorResponse(response, "介绍不能为空。");
			return;
		}
		BizInfo bizInfo = (BizInfo) JsonUtils.json2Object(bizInfoJson,
				BizInfo.class);
		bizInfoService.create(bizInfo);
	}

	@RequestMapping("update")
	public void update(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("bizInfo") String bizInfoJson) {
		log.info("修改业务介绍:" + (bizInfoJson != null ? bizInfoJson : null));
		if (bizInfoJson == null) {
			this.sendErrorResponse(response, "介绍不能为空。");
			return;
		}
		BizInfo bizInfo = (BizInfo) JsonUtils.json2Object(bizInfoJson,
				BizInfo.class);
		BizInfo qBizInfo = bizInfoService.read(bizInfo.getId());
		bizInfo.setCreateTime(qBizInfo.getCreateTime());
		bizInfo.setCreator(qBizInfo.getCreator());
		bizInfoService.update(bizInfo);
	}

	@RequestMapping("read")
	public void read(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("id") Long id) {
		log.info("读取业务介绍:" + id);
		BizInfo bizInfo = bizInfoService.read(id);
		String json = JsonUtils.bean2Json(bizInfo);
		log.info("业务介绍信息：" + json);
		this.sendSuccResponse(response, json);
	}

	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("ids") String ids) {
		log.info("删除业务介绍:" + (ids != null ? ids.toString() : null));
		if (ids == null || "".equals(ids)) {
			this.sendErrorResponse(response, "请选择要删除的业务介绍。");
			return;
		}
		String[] idArr = ids.split(",");
		for (String id : idArr) {
			bizInfoService.delete(Long.parseLong(id));
		}
	}
}
